/**
 * @author lteng
 * @time 2022-06-14 20:07:14 
 */

package liteng.study.cn.stuscore.controller;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import liteng.study.cn.stuscore.comm.Page;
import liteng.study.cn.stuscore.entity.User;
import liteng.study.cn.stuscore.service.UserService;
import liteng.study.cn.stuscore.service.impl.UserServiceImpl;
import liteng.study.cn.stuscore.utils.DateUtils;

/**
 * 用户信息请求控制器
 */
@WebServlet(urlPatterns = "/user")
public class UserServlet extends HttpServlet {

	private UserService userService = new UserServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		System.out.println("User-get方法已经启动");

		
		switch (request.getParameter("action")) {
		case "info":
			descInfo(request, response);
			break;
		case "page":
			pageInfo(request, response);
			break;
		case "add":
			addInfo(request, response);
			break;
		case "upd":
			updInfo(request, response);
			break;
		case "del":
			delInfo(request, response);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("User-post方法启动成功");
		doGet(request, response);
	}

	/**
	 * 打开修改用户信息页面
	 * 跳转/pages/user/upd
	 * Parameter：id
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void descInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Integer.valueOf(request.getParameter("id"));
		User user = userService.getOne(id);
		request.setAttribute("user", user);
		request.getRequestDispatcher("/pages/user/upd.jsp").forward(request, response);
	}

	/**
	 * 分页查询用户信息(模糊查询)
	 * Parameter：pageIndex 起始页,pageSize 每页个数,(username 用户名)
	 * 跳转/pages/user/list
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void pageInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageIndex = Integer.valueOf(request.getParameter("pageIndex"));
		int pageSize = Integer.valueOf(request.getParameter("pageSize"));
//		String userName = request.getParameter("userName");
		
		String userName = request.getParameter("userName");
		if (userName!=null) {
			userName = URLDecoder.decode(userName, "utf-8");

		}
		
		Page page=null;
		if (userName != null && !userName.equals("")) {
			 page = userService.getPageUser(pageIndex, pageSize, userName);
			request.setAttribute("dataPage", page);
		} else {
			 page = userService.getPageUser(pageIndex, pageSize);

			request.setAttribute("dataPage", page);

		}
		request.getRequestDispatcher("/pages/user/list.jsp").forward(request, response);
	}

	/**
	 * 添加用户信息
	 * Parameter：username,comm
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userName = request.getParameter("userName");
		String passWord = "123456";
		String comm = request.getParameter("comm");
		String createTime = DateUtils.getNowDate();

		if (isExist(userName)) {
			request.setAttribute("error", "用户已存在");
			request.getRequestDispatcher("/pages/user/add.jsp").forward(request, response);

		} else {
			User user = new User();
			user.setUserName(userName);
			user.setPassWord(passWord);
			user.setComm(comm);
			user.setCreateTime(createTime);
			user.setType(1);

			userService.add(user);

			response.sendRedirect("/stuscore/user?action=page&pageIndex=1&pageSize=10");
		}
	}

	/**
	 * 更新用户信息
	 * Parameter：id,username,comm
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userName = request.getParameter("userName");
		String comm = request.getParameter("comm");
		int id = Integer.valueOf(request.getParameter("id"));

		User user = userService.getOne(id);

		if (!userName.equals(user.getUserName()) && isExist(userName)) {
			request.setAttribute("error", "要修改的用户已存在！");

			request.getRequestDispatcher("/page/user/upd.jsp").forward(request, response);
		} else {
			user.setUserName(userName);
			user.setComm(comm);

			userService.update(user);

			response.sendRedirect("/stuscore/user?action=page&pageIndex=1&pageSize=10");
		}
	}

	/**
	 * 删除用户信息
	 * Parameter：id
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void delInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.valueOf(request.getParameter("id"));

		User user = userService.getOne(id);

		userService.delete(user);

		response.sendRedirect("/stuscore/user?action=page&pageIndex=1&pageSize=10");

	}

	/**
	 * 检查用户信息是否存在
	 * 
	 * @param name 用户名
	 * @return
	 */
	public boolean isExist(String name) {
		boolean flag = userService.isExistUser(name);
		return flag;
	}

}
